﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using EncuentraloSilverlight.ServiceReference2;
using System.Windows.Navigation;
using System.Windows.Browser;
using System.Collections.ObjectModel;

namespace EncuentraloSilverlight
{
	public partial class Registrar : Page
	{
		public Registrar()
		{
			// Required to initialize variables
			InitializeComponent();
		}



        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Service2Client webservice = new Service2Client();
            int contador = 0;
            webservice.FindAllUsersCompleted += (obj, eventarg) =>
            {
                ObservableCollection<User> users = eventarg.Result;

                foreach (User u in users)
                {
                    if (u.Name.Equals(textbox_name.Text))
                    {
                        MessageBox.Show("El usuario ya existe");
                        return;
                    }
                }
                contador++;
                AddUser(contador);
            };

            webservice.FindAllSellersCompleted += (obj, eventarg) =>
            {
                ObservableCollection<Seller> sellers = eventarg.Result;

                if (sellers != null)
                {
                    foreach (Seller s in sellers)
                    {
                        if (s.Name.Equals(textbox_name.Text))
                        {
                            MessageBox.Show("El usuario ya existe");
                            return;
                        }

                    }
                    contador++;
                    AddUser(contador);
                }
            };

            webservice.FindAllUsersAsync();
            webservice.FindAllSellersAsync();

        }

        private void AddUser(int contador)
        {
            if (contador != 2)
                return;

            Service2Client webservice = new Service2Client();

            //Si se está creando un usuario
            if (userbutton.IsChecked.Value == true)
            {
                webservice.FindAllUsersAsync();

               Address address = new Address()
                {
                    City = textbox_city.Text,
                    Commune = textbox_commune.Text,
                    Street = textbox_street.Text,
                    Number = textbox_number.Text
                };

               User user = new User()
               {
                   Name = textbox_name.Text,
                   Mail = textbox_mail.Text,
                   Password = textbox_password.Password
               };

               user.Addresses = new System.Collections.ObjectModel.ObservableCollection<Address>();
               user.Addresses.Add(address);
               Cookie.SetCookie("EncuentraloUser", user.Name);


               webservice.AddUserCompleted += (obj, eventarg) =>
                   {
                       MessageBox.Show("Usuario Agregado con éxito");
                       NavigationService.Navigate(new Uri("/Views/User_View.xaml", UriKind.Relative));
                   };

               webservice.AddUserAsync(user);

            }

            //Si se está creando un vendedor
            else if (sellerbutton.IsChecked.Value == true)
            {

                ServiceReference2.Seller seller = new ServiceReference2.Seller()
                {
                    Name = textbox_name.Text,
                    Password = textbox_password.Password,
                    //Seller no tiene como atributo mail, debería tenerlo--> modificar tabla en base de datos
                };

                ServiceReference2.Address address = new ServiceReference2.Address()
                {
                    City = textbox_city.Text,
                    Commune = textbox_commune.Text,
                    Street = textbox_street.Text,
                    Number = textbox_number.Text,
                    SellerID = seller.SellerID

                };

                seller.Address = address;
                Cookie.SetCookie("EncuentraloUser", seller.Name);

                webservice.AddSellerCompleted += (obj, eventarg) =>
                {
                    MessageBox.Show("Vendedor Agregado con éxito");
                    NavigationService.Navigate(new Uri("/Views/Seller_View.xaml", UriKind.Relative));
                };

                webservice.AddSellerAsync(seller);

            }

            //no eligió ninguno de los dos
            else 
            {
                NavigationService.Navigate(new Uri("/Views/Home.xaml", UriKind.Relative));
            }
        }

	}
}